<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<style>
.table tbody tr td{
            vertical-align: middle;
        }
        </style>
                    <div class="row">
                        <div class="col-sm-12">
                             <div class="box">
                                <div class="box-header with-border">
                                <h2 class="page-header">
                                    <i class="fa fa-globe"></i> 所有订单

                                </h2>

                            

                                </div>
                                <div class="box-body">
                                
                                <div class="mb-10">
                                <?php echo anchor('/backend/orders?state=2','未发货订单','class="btn btn-sm btn-warning btn-flat pull-right"'); ?>
                                    <?php echo form_open(current_url(), array('class' => 'form-inline', 'method'=>'get', 'id' => 'search_goods')); ?>                
                                    
                                              <div class="form-group" style="margin-right:15px;">
                                    <select class="form-control" name="sitetype">
                                        <option value="0">所有类型</option>
                                        <?php foreach($sitetype as $k=>$s):?>
                                        <option value="<?php echo $k;?>"><?php echo $s;?></option>                                        
                                        <?php endforeach;?>
                                    </select>
                                    </div>

                                    
                                    <div class="form-group" style="margin-right:15px;">
                                    <select class="form-control" name="state">
                                        <option value="0">所有状态</option>
                                        <?php foreach($statetype as $k=>$s):?>
                                        <option value="<?php echo $k;?>"><?php echo $s;?></option>                                        
                                        <?php endforeach;?>
                                    </select>
                                    </div>

                                    <div class="form-group" style="margin-right:15px;">
                                    <?php echo form_input($keyword);?>
                                    </div>

                                    <?php echo form_button(array('type' => 'submit', 'class' => 'btn btn-sm btn-primary btn-flat', 'content' => "搜索订单")); ?>
                                    
                                    <?php
                                    if($config["trackapikey"]){

                                        ?>
                                        <button class="btn btn-flat btn-info btn-sm" id="exportsf" style="margin-left:5px;">导出顺丰单号</button>
                                        <?

                                    }
                                    ?>
                                    
                                    <?php echo form_close();?>
                                    
                                    

                                </div>

                                <?php if($loglist):?>
                                    <table class="table table-striped table-hover" style="margin-top:20px;">
                                        <thead>
                                            <tr>
                                                <th width="50">ID</th>
                                                <th width="100" class="text-center">图片</th> 
                                                <th>订单号</th>
                                                <th>状态</th>
                                                <th>来源</th>
                                                <th>时间</th>
                                                <th>金额</th>
                                                <th>备注</th>
                                                <th>快递</th>
                                                <th width="100">操作</th>
                                            </tr>
                                        </thead>
                                        <tbody>
<?php foreach ($loglist as $item):?>
<tr shopid="<?php echo $item["id"]; ?>">
    <td><?php echo $item["id"]; ?></td>
    <td class="imgcol">
<?php
if(isset($item["thumb"]) && $item["thumb"])
{
    ?>
 <img src="<?php echo $item["thumb"];?>" height="80"/>

    <?php
}
?>
    </td>
    <td>
        
<?php
if(in_array($item["siteid"],$spsite))
{
    $extra=json_decode($spsites[$item["siteid"]]["extra"],true);
    $olink="https://".$extra["apihost"]."/admin/orders/".$item["oid"];
    echo '<a href="'.$olink.'" target="_blank">'.$item["ordersn"].'</a>';
}
else
{
    echo $item["ordersn"];
}
?>
</td>
    <td><span class="label <?php echo $statelabel[$item["state"]]; ?>"><?php echo $statetype[$item["state"]];?></span></td>
    <td><a href="/backend/sites/order/index/<?php echo $item["siteid"];?>" target="_blank"><?php echo $sindex[$item["siteid"]];?></a></td>
    <td><?php echo $item["addtime"];?></td>
    <td><?php echo $item["amount"];?></td>
    <td cid="<?php echo $item["id"];?>"><span ondblclick="edit(this,'note');"><?php echo $item["note"]?$item["note"]:"双击添加备注"; ?></span></td>
    <td cid="<?php echo $item["id"];?>"><?php if(!empty($item["shipping"])): ?>
    
    
    <?php
        $shiplist=explode(",",$item["shipping"]);
        $shipone=$shiplist[0];

        if(substr($shipone,0,2)=="SF")
        {
            $link="https://www.sf-express.com/us/en/dynamic_function/waybill/#search/bill-number/".$shipone;
        }
        else
        {
            $link="https://global.cainiao.com/detail.htm?mailNoList=".$shipone."&spm=a3708.7860688.0.d01&lang=en";
        }
    ?>
    <a href="<?php echo $link;?>" target="_blank"><?php echo $shipone;?></a> 
    <a href=" https://t.17track.net/en#nums=<?php echo $shipone;?>" target="_blank">17track</a> 
      <button class="resetorder btn btn-warning btn-xs btn-flat">更换单号</button>
    <?php else:?>
    <span ondblclick="edit(this,'shipping');">双击手动录入单号</span>
    <?php endif;?>
    </td>
    <td>
    <?php
        if($item["state"]==2||$item["state"]==9)
        {

            echo '<span class="btn btn-xs btn-flat btn-info shopping-btn"  itemid="'.$item["id"].'">采购</span>';
        }
    ?>

    <button class="btn btn-xs btn-flat btn-success" onclick="order_detail('<?php echo $item["ordersn"]; ?>',<?php echo $item["id"]; ?>);" >详情</button>
    </td>
</tr>

<?php endforeach;?>
                                        </tbody>
                                    </table>
                                    <?php endif;?>
                                    <?php 
                                    if(empty($loglist)){
                                        echo "<h2 style='text-align:center;padding:50px 0;'>没有查询到数据</h2>";
                                    }
                                    ?>

<?php echo $pager;?>

<div class=" pull-right form-inline" style="line-height:30px;margin-top:25px;">
    <?php echo '共'.$total.'条订单，'.$page.'/'.$pages.'页';?> 

    <select id="pagesize" class="form-control input-sm" style="height:22px;">
        <option value="0">每页数量</option>
        <option value="50">50</option>
        <option value="100">100</option>
        <option value="200">200</option>
        <option value="500">500</option>
    </select>
   
    <input type="text" id="topage" value="" class="form-control input-sm" placeholder='页码' style="margin:0 5px;height:22px;width:45px;"> 
    <?php echo form_button(array('type' => 'submit', 'class' => 'btn btn-xs btn-primary btn-flat', 'id'=>'jumpbtn','content' => "跳转")); ?>
</div>

                                </div>
                            </div>
                         </div>
                    </div>
        


       
<script>



$("#exportsf").on("click",function(event){
        event.preventDefault();
        layer.confirm("确定导出顺丰单号到TrackingMore吗？", function (index) {           
            layer.close(index);


            var ii = layer.load(1, {
                shade: [0.1,'#000'] 
            });

            $.ajax({
                type:"post",
                url:"/backend/orders/exportsf",
                data:"",
                error:function(){
                    layer.msg("内部错误");
                    layer.close(ii);
                },
                success:function(result){
                    layer.msg("单号导入完毕");
                    layer.close(ii);
                },
            });

        }); 

    });


$(document).ready(function(){
    requirejs.config({
        paths: {
            jquery: "../../components/jquery/jquery.min",
            viewer: "../../components/viewer/viewer.min",
        },
        shim: {
            viewer: {deps: ["loadcss!../../components/viewer/viewer.min.css"]},        
        }
    });
    require(['jquery','viewer'], function($){
        $('.imgcol img').viewer({});
    });

    $("button.resetorder").on("click",function(){
        $(this).parent().html('<span ondblclick="edit(this,\'shipping\');">双击手动录入单号</span>');
    });

});




$("span.shopping-btn").on("click",function(){
    itemid=$(this).attr("itemid");

    index = layer.open({
    type: 1,
        title:'ID:'+itemid+'  采购信息',
        skin: 'layui-layer-rim',
        closeBtn: 1,
        area: ['50%', ''],
        shadeClose: true,
        content: '<div style="padding:20px 30px;"><div class="layui-layer-content" style="margin-bottom:10px;"><div class="form-group"><select name="shopfrom" class="form-control input-sm"><option value="">选择采购来源</option><option value="taobao">淘宝</option><option value="1688">1688</option><option value="aliexpress">速卖通</option><option value="amazon">亚马逊</option><option value="other">其它</option></select></div><div class="form-group"><input type="text" name="ordersn" placeholder="输入采购单号,多个单号请用英文逗号隔开" class="form-control input-sm "></div><div class="form-group"><input type="text" name="shipsn" placeholder="输入物流单号(选填)" class="form-control input-sm"><input type="hidden" name="orderid" value="'+itemid+'"/></div><div class="form-group"><button id="actshopping" class="btn btn-flat btn-info">确定提交</button></div></div></div>'
    });
});

$("body").on("click","#actshopping",function(){
    var cid=$("input[name=orderid]").val();
    var source=$("select[name=shopfrom]").val();
    var ordersn=$("input[name=ordersn]").val();
    var shipsn=$("input[name=shipsn]").val();
    if(source.length==0)
    {
        layer.msg("请选择采购来源");
        return;
    }
    if(ordersn.length==0)
    {
        layer.msg("请输入采购订单号");
        return;
    }
    layer.load(1, {
        shade: [0.1,'#000'] //0.1透明度的白色背景
    });
    $.ajax({
        type:"post",
        url:"/backend/sites/order/ajaxshopping",
        dataType:"json",
        data:{"cid":cid,"source":source,"ordersn":ordersn,"shipsn":shipsn},
        error:function(){
            layer.closeAll(); 
        },
        success:function(result){
            if(result["status"]==1)
            {
                window.location.href=window.location.href;
            }
            else
            {
                layer.closeAll(); 
                layer.msg(result["msg"]);
            }
        },
    });    
});



var order_detail=function(title,oid){
    layer.open({
    type: 2,
    title: title+' 订单详情',
    shadeClose: true,
    shade: 0.8,
    area: ['94%', '90%'],
    content: '/backend/sites/order/detail/'+oid
    }); 
}

    function edit(obj,attrName){

        var oldHtml = obj.innerHTML;
        var newobj = document.createElement('input');
        newobj.name = attrName;
        newobj.type = "text";
        newobj.setAttribute("class","form-control input-sm");
        newobj.value = oldHtml;
        console.log(oldHtml);
        if(oldHtml=="双击添加备注"||oldHtml=="双击手动录入单号")
        {
            newobj.value = '';
        }
        var cid = $(obj).parent().attr("cid");     
        obj.innerHTML = '';  
        obj.appendChild(newobj);
        newobj.focus(); 

        $(newobj).keydown(function (event) {
            if (event.keyCode == 13) {
                $(this).trigger("blur");
            }
        });

        $(newobj).blur(function(){
            if(this.value==null || this.value==""){
                obj.innerHTML = oldHtml;
                return false;
            }
            obj.innerHTML = this.value?this.value:oldHtml;       
            if(oldHtml != this.value){
                $.ajax({
                    type:"post",
                    url:"/backend/sites/order/ajaxedit",
                    dataType:"json",
                    data:{"attrName":attrName,"newHtml":this.value,"cid":cid},
                    error:function(){
                    },
                    success:function(result){
                        if(result["status"]!=1)
                        {
                            layer.msg(result["msg"]);
                            obj.innerHTML = oldHtml;
                            return false;
                        }
                    },
                });
            }  

        })
    }



    $("select#pagesize").change(function(){
        pagesize=$("select#pagesize option:selected").val();
        if(pagesize>0)
        {
            var url= window.location.href;
            var newUrl=  changeURLArg(url, "pagesize", pagesize); 
            window.location.href =  newUrl;
        }
    });


    function changeURLArg(url, arg, arg_val) {
        var pattern = arg + '=([^&]*)';
        var replaceText = arg + '=' + arg_val;
        if (url.match(pattern)) {
            var tmp = '/(' + arg + '=)([^&]*)/gi';
            tmp = url.replace(eval(tmp), replaceText);
            return tmp;
        } else {
            if (url.match('[\?]')) {
                return url + '&' + replaceText;
            } else {
                return url + '?' + replaceText;
            }
        }
        return url + '\n' + arg + '\n' + arg_val;
    }
</script>
